/**
 * Created by XWang on 2018-06-20
 */
var request = require("superagent");
var API = require("./support/const");
var service = require("../../service/log")


/**
 * 产品
 * **/
exports.product = {
    list: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };
        let params = req.body;

        request.get(API.product.list)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getList', 'msg': '获取产品列表'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    info: function (req, res, next) {
        let {id} = req.body;
        let header = {
            token: req.cookies.token
        };
        //let params = req.body;
        request.get(API.product.info + id)
            .set(header)
            //.query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'getId', 'msg': '获取产品详情'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    add: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.post(API.product.add)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'add', 'msg': '新建产品'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    disable: function (req, res, next) {
        let {id} = req.body;
        let header = {
            token: req.cookies.token
        };
        //let params = req.body;
        request.put(API.product.disable + id)
            .set(header)
            //.query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '禁用产品'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    enable: function (req, res, next) {
        let {id} = req.body;
        let header = {
            token: req.cookies.token
        };
        //let params = req.body;
        request.put(API.product.enable + id)
            .set(header)
            //.query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '启用产品'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },

    update: function (req, res, next) {
        let header = {
            token: req.cookies.token
        };
        let params = req.body;
        request.put(API.product.update)
            .set(header)
            .query({msg: JSON.stringify(params)})
            .end(function (_req, _res) {
                if (_res.status === 200) {
                    service.save_user_log({'type': 'edit', 'msg': '更新产品'}, req);
                }
                res.json(JSON.parse(_res.text));
            });
    },
};
